package org.jeecg.modules.backstageModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.PriceSystemModule.entity.PriceInervalEntity;
import org.jeecg.modules.PriceSystemModule.mapper.PriceManagementMapper;
import org.jeecg.modules.backstageModule.entity.CategoryTechnologyEntity;
import org.jeecg.modules.backstageModule.entity.DiscountEntity;
import org.jeecg.modules.backstageModule.entity.FabricGroupEntity;
import org.jeecg.modules.backstageModule.mapper.DiscountMapper;
import org.jeecg.modules.backstageModule.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class DiscountServiceImpl implements DiscountService {

    @Autowired
    DiscountMapper mapper;

    @Autowired
    PriceManagementMapper pricemapper;

    /*
    查询客户折扣
     */
    @Override
    public IPage<DiscountEntity> getCustDiscountList(Page<DiscountEntity> page, String discountSystemName) {
        IPage<DiscountEntity> list = mapper.getCustDiscountList(page, discountSystemName);

        if (list != null) {
            for (int i = 0; i < list.getRecords().size(); i++) {
                list.getRecords().get(i).setDiscountRange(list.getRecords().get(i).getRangeStart() + "-" + list.getRecords().get(i).getRangeEnd());
            }
        }

        return list;
    }


    //设置折扣
    @Override
    public int setDiscount(String userId, String discount, String discountObject, String discountType) {
        return mapper.setDiscount(userId, discount, discountObject, discountType, new Date());
    }


    //查询有一年消费金额的那个列表
    @Override
    public IPage<DiscountEntity> getCustxiaofeiList(Page<DiscountEntity> page) {
        return mapper.getCustxiaofeiList(page);
    }


    @Override
    public int adddiscountSystem(DiscountEntity entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        String id = String.valueOf(System.currentTimeMillis());

        //新增一个折扣体系后，需要将之前存在的所有面料组,都与之关联起来
       //查询所有面料组id
        List<Integer> groupid=mapper.getallgourpId();
         if(groupid!=null){
             for (int i=0;i<groupid.size();i++){
                 mapper.insertgourpandDiscountSystem(groupid.get(i),id,new Date());
             }
         }

        entity.setId(id);

        return mapper.adddiscountSystem(entity);
    }


    //查询设置折扣中的品类工艺折扣列表
    @Override
    public IPage<CategoryTechnologyEntity> getSetdiscountList(Page<CategoryTechnologyEntity> page, String discountId, Integer category, Integer orderType) {
        IPage<CategoryTechnologyEntity> pageList=mapper.getSetdiscountList(page, discountId,category,orderType);
        if (pageList != null)
            for (int i = 0; i < pageList.getRecords().size(); i++) {
                //查询这个是否存在折扣
                String currentzhekou=mapper.getpricezheke(discountId,pageList.getRecords().get(i).getId());
                if(currentzhekou!=null)
                    pageList.getRecords().get(i).setDiscountRatio(currentzhekou);
                else
                    pageList.getRecords().get(i).setDiscountRatio("1");
                String[] fuleiId = pageList.getRecords().get(i).getTopid().split(",");
                String parentName ="";
                for(int j=0;j<fuleiId.length;j++){
                    if(j==0)
                        parentName=parentName+pricemapper.getpartentNameBytopId(fuleiId[j]);
                    if(j>0)
                        parentName=parentName+","+pricemapper.getpartentNameBytopId(fuleiId[j]);

                }
                pageList.getRecords().get(i).setParentName(parentName);
            }

        return pageList;
    }


    //查询折扣用户列表
    @Override
    public IPage<DiscountEntity> getDiscountCustomers(Page<DiscountEntity> page, String companyName, String companyCode, String discountId) {
        return mapper.getDiscountCustomers(page,companyName,companyCode,discountId);
    }

    //删除用户折扣
    @Override
    public int deleteCustomerDiscount(int id) {
        return mapper.deleteCustomerDiscount(id);
    }

    //将客户添加到折扣内
    @Override
    public int addUsertoDiscount(int userId, String discountId) {
        return mapper.addUsertoDiscount(userId, discountId);
    }


    //某个类别下的某个折扣的设置
    @Override
    public int shezhizhekou(Object id, Object discountRatio, Object discountId) {

        //查询这个是否存在折扣
        String currentzhekou=mapper.getpricezheke((String)discountId,(Integer)id);
        if(currentzhekou==null)
            return mapper.insertdisprice((String)discountId,(Integer)id,discountRatio,new Date());
        else
            return mapper.shezhizhekou((Integer)id, (String)discountRatio,discountId);
    }



    //查询所有的品类
    @Override
    public List<HashMap<String, Object>> getallcategory() {
        return mapper.getallcategory();
    }


    //查询面料折扣体系
    @Override
    public IPage<CategoryTechnologyEntity> getmianliaodiscountsystemList(Page<CategoryTechnologyEntity> page, String discountId, String fabricDiscountSystemName) {
        return mapper.getmianliaodiscountsystemList(page,discountId,fabricDiscountSystemName);
    }

    //查询这个面料折扣体系下的所有阶梯折扣
    @Override
    public List<PriceInervalEntity> getmianliaojietidiscountlist(String fabirDiscountSystemId) {
        return mapper.getmianliaojietidiscountlist(fabirDiscountSystemId);
    }

    //查询这个区间的面料阶梯折扣是否已经设置
    @Override
    public String getoldjieti(String fabirDiscountSystemId, int minNumbers, int maxNumbers) {
        return mapper.getoldjieti(fabirDiscountSystemId,minNumbers,maxNumbers);
    }

    //新增一条面料阶梯折扣数据
    @Override
    public int addfabicDiscountjieti(PriceInervalEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addfabicDiscountjieti(entity);
    }

    //假删除这条面料阶梯折扣数据
    @Override
    public int deletemianliaojieti(int id) {
        return mapper.deletemianliaojieti(id);
    }

    //查询所有的面料组
    @Override
    public IPage<FabricGroupEntity> getallmianliaogroup(Page<FabricGroupEntity> page, String groupName) {
        return mapper.getallmianliaogroup(page,groupName);
    }

    //将这个面料组添加到面料折扣体系中
    @Override
    public int addgrouptodiscountsystem(Integer integer, Integer fabirDiscountSystemId, String discountId) {

        //查看这个面料组是否已经存在过这个折扣体系中的面料折扣体系中
        String cunzai=mapper.getgourpindiscountsystem(integer,fabirDiscountSystemId,discountId);
        if(cunzai==null) //然后不存在，则将面料组插入到这个面料折扣体系中
            return  mapper.addgrouptodiscountsystem(integer,fabirDiscountSystemId,new Date());
        else //如果存在，则改变该面料组的所属面料折扣体系
            return mapper.updategrouptodiscountsystem(integer,fabirDiscountSystemId, discountId);

    }

    //添加一个面料折扣体系
    @Override
    public int addmiantiaoDiscountSystem(CategoryTechnologyEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addmiantiaoDiscountSystem(entity);
    }

    //删除面料折扣体系
    @Override
    public int deleteFabricDiscountSystembyId(int id) {

        //删除这个折扣体系id下所有的面料组
         mapper.deleteFabricDiscountGroup(id);

         //删除这个面料折扣体系id下所有的阶梯折扣
         mapper.deleteFabirLadderDiscount(id);

        //删除面料折扣体系
        return mapper.deleteFabricDiscountSystembyId(id);
    }

    /**
     * 查询这个折扣体系下所有的面料组折扣
     * @param page
     * @param id
     * @param groupName
     * @param groupCode
     * @return
     */
    @Override
    public IPage<DiscountEntity> getfabricGroupDiscountbyDiscountId(Page<DiscountEntity> page, String id, String groupName, String groupCode) {
        return mapper.getfabricGroupDiscountbyDiscountId(page,id,groupName,groupCode);
    }

    /**
     * 修改某折扣体系下的面料组折扣
     * @param id
     * @param discountRatio
     * @return
     */
    @Override
    public int updateFabricGroupDiscount(Object id, Object discountRatio) {
        return mapper.updateFabricGroupDiscount(Integer.parseInt(String.valueOf(id)),String.valueOf(discountRatio));
    }

    /**
     * 查询客户折扣体系详情
     * @param id
     * @return
     */
    @Override
    public DiscountEntity getdiscountSystemdetail(String id) {
        return mapper.getdiscountSystemdetail(id);
    }


    /**
     * 修改折扣体系信息
     * @param entity
     * @return
     */
    @Override
    public int updatediscountSystem(DiscountEntity entity) {
        entity.setUpdateTime(new Date());
        return mapper.updatediscountSystem(entity) ;
    }

    /**
     *删除折扣体系
     * @param id
     * @return
     */
    @Override
    public int deletediscountSystem(String id) {
        return mapper.deletediscountSystem(id);
    }


}
